package shuntianfu.com.shuntianfucompressor;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Pattern;

import business.LoginManager;
import common.AppContext;
import common.Constants;
import model.User;
import ui.ClearEditText;
import ui.LoadingProgressDialog;
import utils.CustomToast;
import utils.DataHandler;
import utils.ViewUtil;

public class ModifyPasswordActivity extends AppCompatActivity implements View.OnClickListener, ClearEditText.ChecTextkNull {

    private LinearLayout back;
    private ClearEditText oldPassword;
    private ClearEditText newPassword;
    private ClearEditText againNewPassword;

    private Button submit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_password);

        initView();

    }

    private void initView() {
        back = (LinearLayout) findViewById(R.id.common_title_left);
        back.setVisibility(View.VISIBLE);
        oldPassword = (ClearEditText) findViewById(R.id.setting_old_password);
        newPassword = (ClearEditText) findViewById(R.id.setting_new_password);
        againNewPassword = (ClearEditText) findViewById(R.id.setting_again_password);
        submit = (Button) findViewById(R.id.btn_submit_new_password);

        submit.setEnabled(false);

        oldPassword.setChecTextkNull(this);
        newPassword.setChecTextkNull(this);
        againNewPassword.setChecTextkNull(this);
        back.setOnClickListener(this);
        submit.setOnClickListener(this);

        //可能由于界面为加载完全而无法弹出软键盘。此时应该适当的延迟弹出软键盘
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                //可能由于界面为加载完全而无法弹出软键盘。此时应该适当的延迟弹出软键盘
                ViewUtil.showSoftInput(ModifyPasswordActivity.this, oldPassword);
            }
        }, 200);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_title_left:
                finish();
                overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right);
                break;
            case R.id.btn_submit_new_password:
                submit();
                break;
            default:
                break;
        }
    }

    private void submit() {
        //联网请求 完成后自动关闭
        showCustomProgrssDialog();
        if (!TextUtils.equals(newPassword.getText().toString(), againNewPassword.getText().toString())) {
            CustomToast.showToast(this, "两次输入密码不同", 2000);
            return;
        }
        if (newPassword.getText().toString().trim().length() < 6) {
            CustomToast.showToast(this, "密码最少6位", 2000);
            return;
        }
        if (newPassword.getText().toString().trim().length() > 16) {
            CustomToast.showToast(this, "密码最长16位", 2000);
            return;
        }
        if (!checkPassword(newPassword.getText().toString().trim())) {
            CustomToast.showToast(this, "密码不符合规范", 2000);
            return;
        }
        if (null != AppContext.getInstance().getUser()) {
            User user = AppContext.getInstance().getUser();
            LoginManager.getInstance().modifyPassword(this, String.valueOf(user.getId()), user.getUserName(), oldPassword.getText().toString(), newPassword.getText().toString().trim(), new DataHandler() {
                @Override
                public void onData(int code, String msg, Object obj) {
                    hideCustomProgressDialog();
                    if (code == Constants.SERVER_SUCCESS) {
                        CustomToast.showToast(ModifyPasswordActivity.this, "修改成功", 2000);
                        finish();
                        overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right);
                    } else {
                        CustomToast.showToast(ModifyPasswordActivity.this, "修改失败" + msg, 2000);
                    }
                }
            });
        }
    }

    @Override
    public void checkNull() {
        if (!oldPassword.isNull() && !newPassword.isNull() && !againNewPassword.isNull()) {
            submit.setEnabled(true);
        } else {
            submit.setEnabled(false);
        }
    }


    public static boolean checkPassword(String strPassword) {
        if (strPassword != null) {
            Pattern pattern = Pattern
                    .compile("^([A-Za-z0-9!@#$%^&*.~/{}|()'\\\"?><,.`+-=_:;\\\\\\\\[]]\\\\\\\\\\\\\\[]{6,16})$");
            return pattern
                    .matcher(strPassword).matches();
        }
        return false;
    }

    protected LoadingProgressDialog loadingProgressDialog;

    public void showCustomProgrssDialog() {

        if (null == loadingProgressDialog)

            loadingProgressDialog = LoadingProgressDialog.createDialog(this);

        loadingProgressDialog.setCanceledOnTouchOutside(false);//点击无效，回退有效

//        loadingProgressDialog.setCancelable(false);//点击无效，回退无效

    }

    public void hideCustomProgressDialog() {

        if (null != loadingProgressDialog) {
            loadingProgressDialog.dismiss();
            loadingProgressDialog = null;

        }

    }


}
